﻿using System.Collections.Generic;
using System.Data;
using System;
using SXT.Party.Utility;

namespace ZHDJ.MobileInterface
{
    public partial class GetWXScore : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            GetWXScoreOutParam result = new GetWXScoreOutParam();
            GetWXScoreInParam param = new GetWXScoreInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as GetWXScoreInParam;

                if (string.IsNullOrEmpty(param.UserID))
                {
                    result.code = "0";
                    result.desc = "用户名不能为空。";

                    isFlag = false;
                }

                if (string.IsNullOrEmpty(param.TypeName))
                {
                    result.code = "0";
                    result.desc = "积分类型名称不能为空。";

                    isFlag = false;
                }

                if (isFlag)
                {
                    DataSet ds = InterfaceHelper.GetScoreCode(param.TypeName);

                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "积分编码类型不存在";

                        isFlag = false;
                    }
                    else if (ds.Tables[0].Rows.Count > 1)
                    {
                        result.code = "0";
                        result.desc = "积分编码类型有重复，请联系党建平台管理员";

                        isFlag = false;
                    }
                }
                // }
                if (param.page_no * param.page_count <= 0)
                {
                    result.code = "0";
                    result.desc = "当前页,以及页记录条数必须为大于0的正整数";

                    isFlag = false;
                }
                ///登陆ID
                string loginID = param.UserID;
                //if (string.IsNullOrEmpty(loginID = InterfaceHelper.GetLoginID(param.UserID)))
                //{
                //    result.code = "0";
                //    result.desc = "用户名异常，请确认。";

                //    isFlag = false;
                //}
                if (isFlag)
                {
                    //获取单项总分
                    DataSet dsSignle = InterfaceHelper.GetsingleScore(loginID, param.TypeName);
                    if (!string.IsNullOrEmpty(param.TypeName) || dsSignle != null)
                    {
                        result.ScoreList.SingleAllScore = dsSignle.Tables[0].Rows[0]["sum"].ToString();
                    }

                    DataSet ds = InterfaceHelper.GetScoreList(param.page_no, param.page_count, loginID, "PartyWXBD");
                    if (ds != null && ds.Tables.Count == 2)
                    {
                        ScoreList itemTemp;
                        //result.dataList.total_pages = CommonUtils.GetTotalPages(Convert.ToInt32(ds.Tables[0].Rows[0][0]), param.page_count);
                        foreach (DataRow dr in ds.Tables[1].Rows)
                        {
                            itemTemp = new ScoreList();
                            itemTemp.Score = dr["Score"].ToString();
                            itemTemp.ScoreDescription = dr["Description"].ToString();
                            itemTemp.ScoreTime = dr["ScoreDate"].ToString();
                            itemTemp.ScoreType = "WXBD";

                            result.dataList.list.Add(itemTemp);
                        }
                    }
                    DataSet dsEx = InterfaceHelper.GetScoreList(param.page_no, param.page_count, loginID, "PartyWXGZ");
                    if (ds != null && ds.Tables.Count == 2)
                    {
                        ScoreList itemTemp;
                        //result.dataList.total_pages = CommonUtils.GetTotalPages(Convert.ToInt32(ds.Tables[0].Rows[0][0]), param.page_count);
                        foreach (DataRow dr in dsEx.Tables[1].Rows)
                        {
                            itemTemp = new ScoreList();
                            itemTemp.Score = dr["Score"].ToString();
                            itemTemp.ScoreDescription = dr["Description"].ToString();
                            itemTemp.ScoreTime = dr["ScoreDate"].ToString();
                            itemTemp.ScoreType = "WXGZ";

                            result.dataList.list.Add(itemTemp);
                        }
                    }
                }
                if (isFlag)
                {
                    //获取总分
                    DataSet ds = InterfaceHelper.GetsingleScore(loginID);

                    if (ds != null)
                    {
                        result.ScoreList.AllScore = ds.Tables[0].Rows[0]["sum"].ToString();
                    }

                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-GetWXScore", SerializeHelper.JsonSerializer<GetWXScoreInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<GetWXScoreInParam, GetWXScoreOutParam>();
            else
                SafeProcessRequest<GetWXScoreInParam, GetWXScoreOutParam>();
        }
    }


    #region 请求参数
    public class GetWXScoreInParam
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserID
        { get; set; }

        /// <summary>
        /// 积分类型
        /// </summary>
        public string TypeName
        { get; set; }

        /// <summary>
        /// 必填 当前页
        /// </summary>
        public int page_no
        { get; set; }

        /// <summary>
        /// 必填，每页记录条数
        /// </summary>
        public int page_count
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class GetWXScoreOutParam
    {
        public GetWXScoreOutParam()
        {
            code = "1";
            desc = "操作成功";
            dataList = new ScoreListList();
            ScoreList = new ALLScoreSum();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        public ScoreListList dataList
        { get; set; }

        public ALLScoreSum ScoreList
        { get; set; }

    }
    #region 获取积分详细
    public class ScoreListList
    {
        public ScoreListList()
        {
            total_pages = 0;
            list = new List<ScoreList>();
        }

        public List<ScoreList> list { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int total_pages
        { get; set; }
    }

    /// <summary>
    /// 详情
    /// </summary>
    public class ScoreList
    {
        /// <summary>
        /// 积分
        /// </summary>
        public string Score
        { get; set; }
        /// <summary>
        /// 积分说明
        /// </summary>
        public string ScoreDescription
        { get; set; }

        /// <summary>
        /// 积分时间
        /// </summary>
        public string ScoreTime
        { get; set; }

        /// <summary>
        /// 积分类型 PartyWXGZ 微信关注 PartyWXBD 微信绑定
        /// </summary>
        public string ScoreType
        { get; set; }
    }
    #endregion

    #region 获取用户总积分及单项总积分
    public class ALLScoreSum
    {


        /// <summary>
        /// 总积分
        /// </summary>
        public string AllScore
        { get; set; }

        /// <summary>
        /// 单项总积分
        /// </summary>
        public string SingleAllScore
        { get; set; }

    }

    #endregion
    #endregion
}